<template>
  <el-dialog
    :title="dptForm.id ? '编辑部门' : '新增部门'"
    :visible="dialogVisible"
    width="30%"
    @close="close"
    @before-close="close"
  >
    <el-form ref="dptForm" :model="dptForm" :rules="rules" label-width="120px">
      <el-form-item prop="name" label="部门名称">
        <el-input v-model="dptForm.name" size="mini" placeholder="2-10个字符" style="width: 80%;" />
      </el-form-item>
      <el-form-item prop="code" label="部门编码">
        <el-input v-model="dptForm.code" size="mini" placeholder="2-10个字符" style="width: 80%;" />
      </el-form-item>
      <el-form-item prop="managerId" label="部门负责人">
        <el-select v-model="dptForm.managerId" size="mini" placeholder="请选择负责人" style="width: 80%;">
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="introduce" label="部门介绍">
        <el-input v-model="dptForm.introduce" size="mini" type="textarea" :rows="4" placeholder="1-100个字符" style="width: 80%;" />
      </el-form-item>
      <el-form-item>
        <!-- 按钮 -->
        <el-row type="flex" justify="center">
          <el-col :offset="4">
            <el-button size="mini" type="primary" @click="submitDpt">确定</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addDepartmentApi, getDepartmentDetailApi, getManagerListApi, updateDepartmentDetailApi } from '@/api/department'

export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    nameArr: {
      type: Array,
      default: () => []
    },
    codeArr: {
      type: Array,
      default: () => []
    },
    currentNodeId: {
      type: String,
      default: ''
    }

  },
  data() {
    return {
      dptForm: {
        code: '', // 部门编码
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人名字
        name: '', // 部门名称
        pid: '' // 部门父级部门id
      },
      rules: {
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          {
            min: 2, max: 10, message: '部门编码的长度为2-10个字符', trigger: 'blur'
          },
          {
            trigger: 'blur',
            validator: (rule, value, callback) => {
              if (this.codeArr.includes(value) && !this.dptForm.id) {
                callback(new Error('部门编码已存在'))
              } else {
                callback()
              }
            }
          }
        ], // 部门编码
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          {
            min: 2, max: 100, message: '部门介绍的长度为1-100个字符', trigger: 'blur'
          }
        ], // 部门介绍
        managerId: [
          { required: true, message: '请选择负责人', trigger: 'blur' }
        ], // 部门负责人名字
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          {
            min: 2, max: 10, message: '部门名称的长度为2-10个字符', trigger: 'blur'
          },
          {
            trigger: 'blur',
            validator: (rule, value, callback) => {
              if (this.nameArr.includes(value) && !this.dptForm.id) {
                callback(new Error('部门编码已存在'))
              } else {
                callback()
              }
            }
          }
        ] // 部门名称
      },
      managerList: []
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    close() {
      this.$emit('update:dialogVisible', false)
      this.$refs.dptForm.resetFields() // 仅重置模版中的数据
      this.$nextTick(() => {
        this.dptForm = {
          code: '', // 部门编码
          introduce: '', // 部门介绍
          managerId: '', // 部门负责人名字
          name: '', // 部门名称
          pid: '' // 部门父级部门id
        }
      })
    },
    async getManagerList() {
      const res = await getManagerListApi()
      this.managerList = res
    },
    submitDpt() {
      this.$refs.dptForm.validate(async valid => {
        if (valid) {
          if (this.dptForm.id) {
            // 修改部门
            await updateDepartmentDetailApi(this.dptForm)
          } else {
            this.dptForm = {
              ...this.dptForm,
              pid: this.currentNodeId
            }
            // 添加部门
            await addDepartmentApi(this.dptForm)
          }
          this.$message.success(this.dptForm.id ? '修改成功' : '添加成功')
          this.$emit('render') // 通知父组件刷新
          this.close()
        }
      })
    },
    // 获取部门详情
    async getDptDetail(id) {
      const res = await getDepartmentDetailApi(id)
      this.dptForm = res
    }
  }
}
</script>

<style>

</style>
